﻿using System;
using System.Collections.ObjectModel;

namespace AzureVMDashboard.Model
{
    public class AzureSubscription : ModelBase
    {
        public AzureSubscription(AzureVMController controller) {
            Controller = controller;            
            VMs = new ObservableCollection<AzureVM>();
            LastUpdate = DateTime.MinValue;
        }

        public String ID
        {
            get;
            set;
        }

        public  AzureVMController Controller { get; private set; }

        public virtual ObservableCollection<AzureVM> VMs
        {
            get;
            set;
        }


        private DateTime _LastUpdate;
        public DateTime LastUpdate
        {
            get { return _LastUpdate; }
            set
            {
                if (value != _LastUpdate)
                {
                    _LastUpdate = value;
                    RaisePropertyChanged("LastUpdate");
                }
            }
        }

        private String _name;
        public String Name
        {
            get { return _name; }
            set {
                if (value != _name)
                {
                    _name = value;
                }
            }
        }

        /// <summary>
        /// Two Subscription are equal if have the same Name
        /// </summary>
        /// <param name="obj"></param>
        public override bool Equals(object obj)
        {
            AzureSubscription target = obj as AzureSubscription;
            if (target != null)
                return target.Name == this.Name;

            return false;
        }

        public override string ToString()
        {
            return Name;
        }

        public void RetrieveVMs()
        {
            LastUpdate = DateTime.Now;
            Controller.RetrieveVMsAsync(this);
        }

        public void StartAll() {
            Controller.StartAllAsync(this);
        }
        
        public void StopAll()
        {
            Controller.StopAllAsync(this);
        }

    }
}

